///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef __BUOLA_THREADS_CSYNCVAR_H__
#define __BUOLA_THREADS_CSYNCVAR_H__

#include <buola/buola.h>

namespace buola {

///\ingroup utility threads
///@{

////////////////////////////////////////////////////////////////////////////

/// synchronized variable

/// holds a variable of type tType and provides atomic access to it
///
////////////////////////////////////////////////////////////////////////////

template<typename tType> class CSyncVar
{
public:
//! \name constructor and destructor
//@{
    //! leaves the variable uninitialized
    CSyncVar()
    {
    }

    //! sets the variable to pVar
    CSyncVar(tType pVar)
    {
        mVar=pVar;
    }
//@}

    //! atomically returns the value
    tType Get()
    {
        lock_guard lLock(mMutex);
        return mVar;
    }

    //! atomically sets the value
    void Set(const tType &pValue)
    {
        lock_guard lLock(mMutex);
        mVar=pValue;
    }

    //! atomically sets the value and returns the previous one
    tType GetAndSet(tType pValue)
    {
        lock_guard lLock(mMutex);
        tType lCopy=mVar;
        mVar=pValue;
        return lCopy;
    }

    //! atomically sets the value if its current value is pComp
    tType TestAndSet(tType pComp,tType pValue)
    {
        lock_guard lLock(mMutex);
        if(mVar==pComp)
        {
            mVar=pValue;
        }
        return mVar;
    }

    //! atomically sets the value if its current value is not pComp
    tType NotTestAndSet(tType pComp,tType pValue)
    {
        lock_guard lLock(mMutex);
        if(mVar!=pComp)
        {
            mVar=pValue;
        }
        return mVar;
    }

    //! unsafe access to the variable

    //! before using this function, the mutex should be locked
    tType &Var()
    {
        return mVar;
    }

    //! access to internal mutex
    mutex &Mutex() {   return mMutex;  }

private:
    mutex mMutex;
    tType mVar;
};

///@}

/*namespace buola*/ }

#endif
